package com.wanwanbookstore.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 处理用户退出登录功能的Servlet
 */
public class LogoutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前会话但不创建新会话，更安全的实现方式
        HttpSession session = request.getSession(false);
        if (session != null) {
            // 从session中移除用户信息和管理员标识
            session.removeAttribute("user");
            session.removeAttribute("isAdmin");
            
            // 使整个session失效
            session.invalidate();
        }
        
        // 重定向到首页
        response.sendRedirect("index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // POST请求也调用doGet方法，保持一致的处理逻辑
        doGet(request, response);
    }
}